//
//  ViewController.m
//  03-30上传Demo
//
//  Created by student on 16/3/30.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

#define kBoundary @"suoyimcc" //分割字符串
#define kNewLine @"\r\n" //换行


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self uploadFile];
}

//上传数据需要构建请求体内容

//--kBoundary
//Content-Disposition: form-data; name="file"; filename="文件名"
//Content-Type: text/plain  ->此处text是plain的类型
//
//文件内容
//--kBoundary
//Content-Disposition: form-data; name="username"
//
//用户名
//--kBoundary
-(void)uploadFile
{
    //请求的网址
    NSURL *url=[NSURL URLWithString:@"http://192.168.13.31:8080/MJServer/upload"];
    
    //请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //POST
    request.HTTPMethod = @"POST";
    
    //设置请求头
    NSString *headerStr = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",kBoundary];
    
    [request setValue:headerStr forHTTPHeaderField:@"Content-Type"];
    
//    ---------------------------------------------
    //请求体
    NSMutableData *mData = [NSMutableData data];
    NSMutableString *mStr =[NSMutableString string];
    
    //1.文件参数
    [mStr appendString:kNewLine];
    [mStr appendString:@"--"];
    [mStr appendString:kBoundary];
    [mStr appendString:kNewLine];
    [mStr appendString:@"Content-Disposition: form-data; name=\"file\"; filename=\"软技能主题.txt\""];
    [mStr appendString:kNewLine];
    [mStr appendString:@"Content-Type: text/plain"];
    //文件类型对应的Content-Type image/jpeg image/png
    
    [mStr appendString:kNewLine];
    [mStr appendString:kNewLine];
    NSLog(@"%@",mStr);
    [mData appendData:[mStr dataUsingEncoding:NSUTF8StringEncoding]];
    
    //2.文件的数据
    NSString *filepath =[[NSBundle mainBundle]pathForResource:@"软技能主题 "ofType:@"txt"];
    
    NSData *data=[NSData dataWithContentsOfFile:filepath];
    [mData appendData:data];
    
    //3.其他参数
    mStr = [NSMutableString string];
    [mStr appendString:kNewLine];
    [mStr appendString:@"--"];
    [mStr appendString:kBoundary];
    [mStr appendString:kNewLine];
    [mStr appendString:@"Content-Disposition: form-data; name=\"username\""];
    [mStr appendString:kNewLine];
    [mStr appendString:kNewLine];
    [mStr appendString:@"123"];
    [mStr appendString:kNewLine];
    
    //4.结果
    [mStr appendString:@"--"];
    [mStr appendString:kBoundary];
    [mStr appendString:@"--"];
    [mStr appendString:kNewLine];
    NSLog(@"%@",mStr);
    [mData appendData:[mStr dataUsingEncoding:NSUTF8StringEncoding]];
    
    request.HTTPBody = mData ;
//    -----------------------------------------------------------------
    
    // 发送请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if(connectionError == nil)
        {
            NSError *error;
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
            if(error== nil)
            {
                NSLog(@"%@",dict);
            }
            else
            {
                NSLog(@"%@",[error localizedDescription]);
            }
        }
        else
        {
            NSLog(@"%@",[connectionError localizedDescription]);
        }
    }];

    
    
}

@end
